<template>
  <div id="bar-chart" :class="className" :style="{ height:height,width:width }" />
</template>

<script lang="ts" setup>
import * as echarts from 'echarts'
import resize from '@/utils/resize'
import { nextTick, onMounted, onUnmounted } from 'vue'
const { chart, mounted, beforeDestory } = resize()

let echart = echarts
const props = defineProps({
  className: {
    type: String,
    default: 'chart'
  },
  width: {
    type: String,
    default: '100%'
  },
  height: {
    type: String,
    default: '300px'
  }
})

const initChart = () => {
  if (chart.value) {
    chart.value.setOption({
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      grid: {
        top: 10,
        left: '2%',
        right: '2%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [{
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      }],
      yAxis: [{
        type: 'value'
      }],
      series: [{
        color: ['#57CAEB', '#5DDAB4', '#57CAEB', '#435EBE'],
        name: 'pageA',
        type: 'bar',
        stack: 'vistors',
        emphasis: {
          focus: 'series'
        },
        data: [79, 52, 200, 334, 390, 330, 220]
      }, {
        name: 'pageB',
        type: 'bar',
        stack: 'vistors',
        emphasis: {
          focus: 'series'
        },
        data: [80, 52, 200, 334, 390, 330, 220]
      }, {
        name: 'pageC',
        type: 'bar',
        stack: 'vistors',
        emphasis: {
          focus: 'series'
        },
        data: [30, 52, 200, 334, 390, 330, 220]
      }]
    })
  }
}

onMounted(() => {
  chart.value = echart.init(document.getElementById('bar-chart') as HTMLDivElement)
  initChart()
  mounted()
})
onUnmounted(() => {
  beforeDestory()
  chart.value.dispose()
})
</script>